Malware compromises a variety of devices and uses them for malicious intent, such as demanding a ransom, logging key presses to steal password or in other applications such as a Botnet.
Ransomware is designed to encrypt all of your files and demand a ransom to decrypt them, effectively rendering your computer useless if you do not pay the ransom.
Keyloggers are designed to record all the keypresses on your keyboard to steal information that you type such as personal info, passwords or financial information. The attacker can then do whatever they like with this information.
You can stay safe from Malware by not downloading any files from sources that aren't trustworthy. It's also a good idea to have a good anti-malware solution like Malwarebytes.
You can protect from Ransomware by taking redundant backups of your computer and other important files so that you can restore it in the event you get Ransomware. A good anti-malware solution also helps.
You can protect your computer from Keyloggers by ensuring the software you use is trustworthy and also having a good anti-malware solution.